/*
 *  nautilus-svn.c - Subversion extension for Nautilus
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Author: Jack Xu Hong <jack.xu.hong@gmail.com>
 *
 */

#include <config.h>

#include <gtk/gtk.h>
#include <libgnomevfs/gnome-vfs-utils.h>

#include <svn_cmdline.h>

#include "nautilus-svn.h"
#include "nautilus-svn-prompt.h"

static svn_error_t *
nautilus_svn_simple_prompt_callback (svn_auth_cred_simple_t **cred,
                                     void *baton,
                                     const char *realm,
                                     const char *username,
                                     svn_boolean_t may_save,
                                     apr_pool_t *pool)
{
	svn_auth_cred_simple_t *ret = apr_pcalloc (pool, sizeof (*ret));
	NautilusSvnPrompt *prompt;
	gint result;

	prompt = nautilus_svn_prompt_new(NULL);

	if (realm) {
		printf ("Authentication realm: %s\n", realm);
	}

	result = nautilus_svn_prompt_run(prompt, realm, username);
	if (result == GTK_RESPONSE_OK) {
		ret->username = apr_pstrdup (pool,
				nautilus_svn_prompt_get_username(prompt));
		ret->password = apr_pstrdup (pool,
				nautilus_svn_prompt_get_password(prompt));
	}

	*cred = ret;
	return SVN_NO_ERROR;
}

static svn_error_t *
nautilus_svn_username_prompt_callback (svn_auth_cred_username_t **cred,
                                       void *baton,
                                       const char *realm,
                                       svn_boolean_t may_save,
                                       apr_pool_t *pool)
{
	svn_auth_cred_username_t *ret = apr_pcalloc (pool, sizeof (*ret));
	NautilusSvnPrompt *prompt;
	gint result;

	prompt = nautilus_svn_prompt_new(NULL);

	if (realm) {
		printf ("Authentication realm: %s\n", realm);
	}

	result = nautilus_svn_prompt_run(prompt, realm, NULL);
	if (result == GTK_RESPONSE_OK) {
		ret->username = apr_pstrdup (pool,
				nautilus_svn_prompt_get_username(prompt));
	}

	*cred = ret;
	return SVN_NO_ERROR;
}


NautilusSvn *
nautilus_svn_init (void)
{
	NautilusSvn *svn = g_new0 (NautilusSvn, 1);

	svn_cmdline_init ("nautilus-svn", stderr);

	svn->pool = svn_pool_create (NULL);
	svn_client_create_context (&svn->ctx, svn->pool);

	svn_auth_provider_object_t *provider;
	apr_array_header_t *providers =
		apr_array_make (svn->pool, 4, sizeof (svn_auth_provider_object_t *));

	svn_auth_get_simple_prompt_provider (&provider,
										 nautilus_svn_simple_prompt_callback,
										 svn, /* baton */
										 2, /* retry limit */
										 svn->pool);
	APR_ARRAY_PUSH (providers, svn_auth_provider_object_t *) = provider;

	svn_auth_get_username_prompt_provider (&provider,
										   nautilus_svn_username_prompt_callback,
										   svn, /* baton */
										   2, /* retry limit */
										   svn->pool);
	APR_ARRAY_PUSH (providers, svn_auth_provider_object_t *) = provider;

	/* Register the auth-providers into the context's auth_baton. */
	svn_auth_open (&svn->ctx->auth_baton, providers, svn->pool);


	return svn;
}

void
nautilus_svn_destroy (NautilusSvn *svn)
{
	svn_pool_destroy (svn->pool);
	g_free (svn);
}
